<?php
namespace Admin\Controller;
use Think\Controller;
class LifeController extends BaseController {
    private $createlife_fields = array('title', 'cate_id ','city_id','area_id','user_id','photo','subtitle','mobile','addr','views','create_time','create_ip','last_time','audit','lng','lat','content','is_red');
    private $editlife_fields = array('title', 'cate_id ','city_id','area_id','user_id','photo','subtitle','mobile','addr','views','create_time','create_ip','last_time','audit','lng','lat','content','is_red');
    private $create_fields = array('cate_name', 'orderby',);
    private $edit_fields = array('cate_name', 'orderby', );
	private $createSub_fields = array('cate_name', 'orderby','pid',);
	private $editSub_fields = array('cate_name', 'orderby','pid',);
	
    public function index(){
        $life = D('life');
        $life_cate = D('life_cate');
        $count = $life->count(); 
        $p = getpage($count,C('PAGE_SIZE'));
        $show = $p->show(); 
        $list = $life->page(I('get.p'))->order('life_id desc')->limit(C('PAGE_SIZE'))->select();
        $list1 = $life_cate->select();
        $this->assign('page', $show); 
        $this->assign('list', $list);
        $this->assign('list1', $list1);
        $this->display();
    }
 
  
    public function add(){
        $life_cate = D('life_cate');
        $list = $life_cate->select();
		
        if (IS_POST) {
            $data = $this->addCheck();
            $obj = D('life');
            if ($obj->add($data)) {
                $this->success("保存成功", U('Life/index'));
                return;
            }else {
            $this->error('操作失败！');
            return;
         } 
        }
        $this->assign('list', $list);
        $this->display();
    }

    private function addCheck() {
    	
        $param = I('post.');
        $data = $this->checkFields($param["data"], $this->createlife_fields);
        $create_time = strtotime(date("Y-m-d H:i:s",time()));
		$last_time = strtotime(date("Y-m-d H:i:s",time()));
        $data['title'] = trim(I('post.title', '', 'htmlspecialchars'));
        if (D('life')->getLifeTitle($data['title'])) {
            $this->error('该标题已经存在');
            exit;
        }
        if (empty($data['title'])) {
            $this->error('标题不能为空');
        }
        $data['subtitle'] = trim(I('post.subtitle', '', 'htmlspecialchars'));
        if (empty($data['subtitle'])) {
            $this->error('子标题不能为空');
        }
        $data['cate_id'] = trim(I('post.cate_id', '', 'htmlspecialchars'));
        $data['photo'] = trim(I('post.photo', '', 'htmlspecialchars'));
		$data['views'] = trim(I('post.views', '', 'htmlspecialchars'));
		$data['audit'] = trim(I('post.audit', '', 'htmlspecialchars'));
		$data['create_ip'] = get_client_ip();
        $data['content'] = $_POST['content'];	
		 $data['is_red'] = trim(I('post.is_red', '', 'htmlspecialchars'));		
        if (empty($data['content'])) {
            $this->error('内容不能为空');
        }
        $data['mobile'] = trim(I('post.mobile', '', 'htmlspecialchars'));
        if (empty($data['mobile'])) {
            $this->error('手机号码不能为空');
        }
		if (!isPhone($data['mobile'])) {
                $this->error('请输入有效的手机号');
                exit;
        }
        $data['addr'] = trim(I('post.addr', '', 'htmlspecialchars'));
        if (empty($data['addr'])) {
            $this->error('地址不能为空');
        }
        $data['create_time'] = $create_time;
        $data['last_time'] = $last_time;
        return $data;
    }
 
 
  
    public function edit($life_id = 0) {
        if ($life_id = (int) $life_id) {
            $obj = D('life');
            $life_cate = D('life_cate');
            $list = $life_cate->select();
            if (!$detail = $obj->find($life_id)) {
                $this->error('请选择要编辑的信息');
            }
            if (IS_POST) {
                $data = $this->editCheck();
                $data['life_id'] = $life_id;  
                if ($obj->save($data)) {
                    $this->success('操作成功', U('Life/index'));
                } else {
                    $this->error('操作失败');
                }
            } else {
                $this->assign('list', $list);
                $this->assign('detail', $detail);
                $this->display();
            }
        } else {
            $this->error('请选择要编辑的信息');
        }
    }

    private function editCheck() {
        $param = I('post.');
        $data = $this->checkFields($param['data'], $this->editadvert_fields);
        $last_time = strtotime(date("Y-m-d H:i:s",time()));
        $data['title'] = trim(I('post.title', '', 'htmlspecialchars'));
        if (empty($data['title'])) {
            $this->error('标题不能为空');
        }
        $data['subtitle'] = trim(I('post.subtitle', '', 'htmlspecialchars'));
        if (empty($data['subtitle'])) {
            $this->error('子标题不能为空');
        }
        $data['cate_id'] = trim(I('post.cate_id', '', 'htmlspecialchars')); 
        $data['photo'] = trim(I('post.photo', '', 'htmlspecialchars'));
        $data['content'] = $_POST['content'];	
        if (empty($data['content'])) {
            $this->error('内容不能为空');
        }
        $data['mobile'] = trim(I('post.mobile', '', 'htmlspecialchars'));
        if (empty($data['mobile'])) {
            $this->error('手机号码不能为空');
        }
        $data['addr'] = trim(I('post.addr', '', 'htmlspecialchars'));
        if (empty($data['addr'])) {
            $this->error('地址不能为空');
        }
		
		$data['cate_id'] = trim(I('post.cate_id', '', 'htmlspecialchars'));
		$data['city_id'] = trim(I('post.city_id', '', 'htmlspecialchars'));
		$data['area_id'] = trim(I('post.area_id', '', 'htmlspecialchars')); 
		$data['user_id'] = trim(I('post.user_id', '', 'htmlspecialchars')); 
        $data['photo'] = trim(I('post.photo', '', 'htmlspecialchars'));
		$data['lng'] = trim(I('post.lng', '', 'htmlspecialchars'));
		$data['lat'] = trim(I('post.lat', '', 'htmlspecialchars'));
		$data['views'] = trim(I('post.views', '', 'htmlspecialchars'));
		$data['audit'] = trim(I('post.audit', '', 'htmlspecialchars'));
		$data['is_red'] = trim(I('post.is_red', '', 'htmlspecialchars'));		
		$data['create_ip'] = get_client_ip();
        $data['last_time'] = $last_time;
        return $data;
    }                     
 
 
  
    public function del() {
        $life = D('life');
        $life_id = I('get.life_id');
        $result = $life->where('life_id =' . $life_id . '')->delete();
        if ($result) {
            $this->success('删除成功', U('Life/index'));
        } else {
            $this->error('删除失败');
        }
    }


    public function indextype() {
        $Life_cate = D('Life_cate');
        $count = $Life_cate->count();     
        $p = getpage($count,C('PAGE_SIZE'));
        $show = $p->show(); 
        $list = $Life_cate->page(I('get.p'))->limit(C('PAGE_SIZE'))->select();
		$result1 = $Life_cate->where('pid=0')->select();
        $this->assign('list1',$result1);
        $result2 = $Life_cate->where('pid!=0')->select();
        $this->assign('list2',$result2);
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    public function addtype() {
        $obj = D('Life_cate');
        if (IS_POST) {
            $data = $this->createCheck1();
          
            if ($obj->add($data)) {
                $this->success("保存成功", U('life/indextype'));
                return;
            } else {
                $this->error('操作失败！');
                return;
            }
        }
        $this->display();
    }
	


    private function createCheck1() {

        $param = I('post.');
        $data = $this->checkFields($param["data"], $this->create_fields);
        $data['cate_name'] = trim(I('post.cate_name', '', 'htmlspecialchars'));
        $data['orderby'] =  trim(I('post.orderby', '', 'htmlspecialchars'));
		$data['subtitle'] =  trim(I('post.subtitle', '', 'htmlspecialchars'));
		$data['img_url'] =  trim(I('post.img_url', '', 'htmlspecialchars'));
		$data['note'] =  trim(I('post.note', '', 'htmlspecialchars'));
		$data['pid'] = 0;
        if ($data['cate_name'] == null) {
            $this->error('标题不能为空');
            return;
        }
		if ($data['subtitle'] == null) {
            $this->error('子标题不能为空');
            return;
        }
		if ($data['note'] == null) {
            $this->error('说明不能为空');
            return;
        }
        if ($data['orderby'] == null) {
            $this->error('排序不能为空');
            return;
        }
        
        return $data;
    }
	

    public function edittype($cate_id = 0) {
        $obj = D('Life_cate');
        if ($cate_id = (int) $cate_id) {
            if (!$detail = $obj->find($cate_id)) {
                $this->error('请选择要编辑的分类');
            }
            if (IS_POST) {
                $data = $this->editCheck1();
                $data['cate_id'] = $cate_id;
                if ($obj->save($data)) {
                    $this->success('操作成功', U('life/indextype'));
                    return;
                } else {
                    $this->error('操作失败');
                    return;
                }
            } else {

                $this->assign('detail', $detail);
                $this->display();
            }
        } else {
            $this->error('请选择要编辑的分类');
        }
    }

    private function editCheck1() {

        $param = I('post.');
        $data = $this->checkFields($param['data'], $this->edit_fields);
        $data['cate_name'] = trim(I('post.cate_name', '', 'htmlspecialchars'));
        $data['orderby'] =  trim(I('post.orderby', '', 'htmlspecialchars'));
		$data['subtitle'] =  trim(I('post.subtitle', '', 'htmlspecialchars'));
		$data['img_url'] =  trim(I('post.img_url', '', 'htmlspecialchars'));
		$data['note'] =  trim(I('post.note', '', 'htmlspecialchars'));
        if ($data['cate_name'] == null) {
            $this->error('标题不能为空');
            return;
        }
		if ($data['subtitle'] == null) {
            $this->error('子标题不能为空');
            return;
        }
		if ($data['note'] == null) {
            $this->error('说明不能为空');
            return;
        }
        if ($data['orderby'] == null) {
            $this->error('排序不能为空');
            return;
        }
        
        return $data;
    }



     public function deltype() {
        $Life_cate = D('Life_cate');
        $cate_id = I('get.cate_id');
        $result = $Life_cate->where('cate_id =' . $cate_id . '')->delete();
        if ($result) {
            $this->success('删除成功', U('Life/indextype'));
        } else {
            $this->error('删除失败');
        }
    }
	 



	public function addSubtype($cate_id = 0) {
		
		$cate_id = (int)$cate_id;
        $obj = D('Life_cate');

        if (IS_POST) {
        	
            $data = $this->createSubCheck1();         
            if ($obj->add($data)) {
                $this->success("保存成功", U('life/indextype'));
                return;
            } else {
                $this->error('操作失败！');
                return;
            }
        }else{
        	$result = $obj->where('cate_id='.$cate_id.'')->find();
          	$this->assign('list',$result);
        	$this->display();
        }		
    }
	
	private function createSubCheck1() {

        $param = I('post.');
        $data = $this->checkFields($param["data"], $this->createSub_fields);
        $data['cate_name'] = trim(I('post.cate_name', '', 'htmlspecialchars'));
        $data['orderby'] =  trim(I('post.orderby', '', 'htmlspecialchars'));
		$data['pid'] =  trim(I('post.pid', '', 'htmlspecialchars'));
        if ($data['cate_name'] == null) {
            $this->error('子分类名称不能为空');
            return;
        }
        if ($data['orderby'] == null) {
            $this->error('排序不能为空');
            return;
        }
        
        return $data;
    }




    public function editSubtype($cate_id = 0) {
        $obj = D('Life_cate');
        if ($cate_id = (int) $cate_id) {
            if (!$detail = $obj->find($cate_id)) {
                $this->error('请选择要编辑的分类');
            }
            if (IS_POST) {
                $data = $this->editCheck1();
                $data['cate_id'] = $cate_id;
                if ($obj->save($data)) {
                    $this->success('操作成功', U('life/indextype'));
                    return;
                } else {
                    $this->error('操作失败');
                    return;
                }
            } else {

                $this->assign('detail', $detail);
                $this->display();
            }
        } else {
            $this->error('请选择要编辑的分类');
        }
    }

    private function editSubCheck1() {

        $param = I('post.');
        $data = $this->checkFields($param['data'], $this->edit_fields);
        $data['cate_name'] = trim(I('post.cate_name', '', 'htmlspecialchars'));
        $data['orderby'] =  trim(I('post.orderby', '', 'htmlspecialchars'));
		$data['pid'] =  trim(I('post.pid', '', 'htmlspecialchars'));
        if ($data['cate_name'] == null) {
            $this->error('名称不能为空');
            return;
        }
        if ($data['orderby'] == null) {
            $this->error('排序不能为空');
            return;
        }
        
        return $data;
    }




	public function delSubtype($cate_id = 0){
        
        $cate_id =  (int)$cate_id;
        $model = M('Life_cate');
        $result = $model->where('cate_id = '.$cate_id.'')->delete();
        if($result){
            $this->success('删除成功', U('Life/indextype'));
        }else{
            $this->error('删除失败');
        }   
    }

}

      